const app = getApp();
Page({

  // setdata: app.setdata,
  setdata: function (e) {
    var obj = {};
    console.log(e.currentTarget.dataset.key, e.currentTarget.dataset.val)
    obj[e.currentTarget.dataset.key] = e.currentTarget.dataset.val;
    this.setData(obj);

    if (e.currentTarget.dataset.key == "month" && this.data.dj!=0){
      this.setData({
        fee: (this.data.dj * this.data.month).toFixed(2)
      });
    }

  },

  /**
   * 页面的初始数据
   */
  data: {
    uhide: 1,
    month:"3",
    data:{},
    res:{},
    type:0,
    item: {},

    idParkingFeeProject: "",
    parkingFeeProject: [],

    fee:"0.00",
    dj:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    if (options.data!=undefined){
      this.setData({
        data: JSON.parse(options.data)
      })
    }
    
    if (options.res != undefined) {
      this.setData({
        res: JSON.parse(options.res)
      })
    }

    if (options.type != undefined) {
      console.log(JSON.parse(options.item))
      this.setData({
        type: options.type,
        item: JSON.parse(options.item)
      })

      app.api.client_parkingFee_getParkingFeeProjects({
        token: wx.getStorageSync('token'),
        idCommunity: wx.getStorageSync('idCommunity'),
      }, (res) => {

        this.setData({
          parkingFeeProject: res.data
        })
        if (res.status == -1) {
          app.toast(res.msg);
        }
      })

    }

    console.log(options)

    app.init(this);
    
  },


  idParkingFeeProject: function (e) {
    console.log(e.detail)
    this.setData({
      idParkingFeeProject: e.detail.value
    })
  },

  select_type: function (e) {
    console.log(e.currentTarget.dataset.item.dj);

    this.setData({
      dj: e.currentTarget.dataset.item.dj,
      fee: (e.currentTarget.dataset.item.dj * this.data.month).toFixed(2),
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectPrice: function (event) {
    var that = this;
    var selectVal = that.data.uhide;
    var itemId = event.currentTarget.id;
    var price = event.currentTarget.dataset.price;
    console.log(price);
    if (selectVal != itemId) {
      that.setData({
        uhide: itemId
      })

    }
  },

  bindPickerChange: function (e) {
    console.log(e)
    this.setData({
      month: parseInt( e.detail.value )+1
    })

    if (this.data.dj != 0) {
      this.setData({
        fee: (this.data.dj * this.data.month).toFixed(2)
      });
    }


  },
  
  // 支付
  client_parkingFee_pay: function (e) {

    app.api.client_parkingFee_pay({
      token: wx.getStorageSync("token"),
      payOrderNo: this.data.res.payOrderNo,
    }, (res) => {
      if (res.status == -1){
        app.toast(res.msg);
        return false;
      }
      wx.requestPayment({
        timeStamp: String(res.data.timeStamp),
        nonceStr: res.data.nonce_str,
        package: res.data.prepay_id,
        signType: "MD5",
        paySign: res.data.sign,
        success(res) {
          console.log("支付调用结果=>", res);
          app.toast("支付成功");
          wx.setStorageSync("isApplyCar",1)
          wx.navigateBack({
            delta: 3
          })
        },
        fail(res) {
          console.log("支付调用结果 fail=>", res);
          app.toast("支付取消");
        }
      });

    })
    
  },

  //  续费
  client_parkingFee_renewalFee: function (e) {
    if (this.data.idParkingFeeProject == "") {
      app.toast("请选择月卡车类型");
      return false;
    }

    app.api.client_parkingFee_renewalFee({
      token: wx.getStorageSync("token"),
      id: this.data.item.id,
      idParkingFeeProject:this.data.idParkingFeeProject,
      months: this.data.month,
    }, (res) => {
      if (res.status == -1) {
        app.toast(res.msg);
        return false;
      }
      else{
        
        app.api.client_parkingFee_pay({
          token: wx.getStorageSync("token"),
          payOrderNo: res.data.payOrderNo,
        }, (res) => {
          if (res.status == -1) {
            app.toast(res.msg);
            return false;
          }
          wx.requestPayment({
            timeStamp: String(res.data.timeStamp),
            nonceStr: res.data.nonce_str,
            package: res.data.prepay_id,
            signType: "MD5",
            paySign: res.data.sign,
            success(res) {
              console.log("支付调用结果=>", res);
              app.toast("支付成功");
              app.history_back();
            },
            fail(res) {
              console.log("支付调用结果 fail=>", res);
              app.toast("支付取消");
            }
          });

        })


      }





    })

  },
  

  

})